package dao.entity;

import dao.MysqlConfiguration;
import dao.interf.UserDao;
import dao.util.DaoTools;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class MerchantDaoImpl implements UserDao {
    public static int midConf=10000;
    public int addMerchant(String mname,String certifi_path,String m_phone,String m_intro) {
        Connection connection= MysqlConfiguration.MysqlConnection();
        String sql="insert into merchants values(?,?,?,?,?,?,?)";
        System.out.println(sql);
        int n=0;//受影响的行数
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            System.out.println("创建insert商家语句对象成功!");
            midConf+= DaoTools.countPersonSum("merchants") +1;
            pst.setString(1,midConf+"");
            pst.setString(2,mname);
            pst.setString(3,certifi_path);//
            pst.setString(4,m_phone);
            LocalDateTime ldt= LocalDateTime.now();//创建一个当前时间对象
            DateTimeFormatter dtf= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//创建一个日期格式化对象
            String lg_time=dtf.format(ldt);//当前时间
            pst.setString(5,lg_time);
            pst.setInt(6,1);
            pst.setString(7,m_intro);
            n=pst.executeUpdate();
        } catch (SQLException e) {
            System.out.println("创建insert商家语句对象失败:"+e.getMessage());
        }
        return n;
    }

    @Override
    public ResultSet selectUserByName(String name) {
        return null;
    }

    @Override
    public boolean isPhoneExisting(String phone) {
        return false;
    }

    @Override
    public boolean isOnline(String mname) {
        Connection connection=MysqlConfiguration.MysqlConnection();
        String sql="select m_status from merchants where mname=?";
        ResultSet rs=null;
        boolean tag=false;
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            pst.setString(1,mname);
            rs=pst.executeQuery();
        } catch (SQLException e) {
            System.out.println("创建select m_status商家语句对象失败:"+e.getMessage());
        }
        try {
            rs.next();
            tag=rs.getBoolean("m_status");
        } catch (SQLException e) {
            System.out.println("错误信息:"+e.getMessage());
            tag=false;
        }
        return tag;
    }

    @Override
    public int logOut(String name) {
        return 0;
    }
}
